package com.epam.tcs.dao.hibernate.impl;

import java.util.List;

import org.hibernate.Session;

import com.epam.tcs.dao.factory.DAO;
import com.epam.tcs.dao.hibernate.ConnectionFactory;

public class UniversalDAOHibernateImpl<T> extends ElementDAOHibernateImpl<T> implements DAO<T> {

	public UniversalDAOHibernateImpl(Class objType){
		this.objType = objType;
	}
	
	@Override
	public T getElement(long id) {
		Session session = ConnectionFactory.getOrInitSession();
		T t = (T) session.load(objType, id);
		session.close();
		return t;
	}

	@Override
	public List<T> getAllElements() {
		Session session = ConnectionFactory.getOrInitSession();
		List<T> ts = (List<T>) session
				.createQuery("from " + objType.getName()).list();
		session.close();
		return ts;
	}

}
